<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="教练id" prop="coachId">
        <el-input
            v-model="queryParams.coachId"
            placeholder="请输入教练id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练价格" prop="price">
        <el-input
            v-model="queryParams.price"
            placeholder="请输入教练价格"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="好评率" prop="goodReview">
        <el-input
            v-model="queryParams.goodReview"
            placeholder="请输入好评率"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计上课数" prop="classTotalNum">
        <el-input
            v-model="queryParams.classTotalNum"
            placeholder="请输入累计上课数"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分" prop="point">
        <el-input
            v-model="queryParams.point"
            placeholder="请输入评分"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否推荐" prop="ifRecommend">
        <el-input
            v-model="queryParams.ifRecommend"
            placeholder="请输入是否推荐"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:class:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:class:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:class:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:class:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="classList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程id" align="center" prop="id" />
      <el-table-column label="教练id" align="center" prop="coachId" />
      <el-table-column label="教练价格" align="center" prop="price" />
      <el-table-column label="简介" align="center" prop="description" />
      <el-table-column label="好评率" align="center" prop="goodReview" />
      <el-table-column label="累计上课数" align="center" prop="classTotalNum" />
      <el-table-column label="标签" align="center">
        <template #default="scope">
          <el-tag
              v-for="tag in scope.row.tag"
              :key="tag"
              style="margin-right: 5px;"
          >
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="point" />
      <el-table-column label="是否推荐" align="center">
        <template #default="scope">
          {{ scope.row.ifRecommend === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:class:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:class:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改私教课程列表对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="classRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="教练id" prop="coachId">
          <el-input v-model="form.coachId" placeholder="请输入教练id" />
        </el-form-item>
        <el-form-item label="教练价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入教练价格" />
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="好评率" prop="goodReview">
          <el-input v-model="form.goodReview" placeholder="请输入好评率" />
        </el-form-item>
        <el-form-item label="累计上课数" prop="classTotalNum">
          <el-input v-model="form.classTotalNum" placeholder="请输入累计上课数" />
        </el-form-item>
        <el-form-item label="评分" prop="point">
          <el-input v-model="form.point" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="是否推荐" prop="ifRecommend">
          <el-radio-group v-model="form.ifRecommend">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <div>
            <el-input
                v-model="tagInput"
                placeholder="请输入标签"
                clearable
                @keyup.enter="addTag"
                style="width: 200px; margin-right: 10px;"
            />
            <el-button type="primary" @click="addTag">添加标签</el-button>
            <div style="margin-top: 10px;">
              <el-tag
                  v-for="tag in form.tag"
                  :key="tag"
                  closable
                  @close="removeTag(tag)"
                  style="margin-right: 5px;"
              >
                {{ tag }}
              </el-tag>
            </div>
            <div style="color: #999; font-size: 12px; margin-top: 5px;">
              按下回车生成一个标签
            </div>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Class">
import { listClass, getClass, delClass, addClass, updateClass } from "@/api/system/class";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";

const { proxy } = getCurrentInstance();

const classList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const tagInput = ref("");

const data = reactive({
  form: {
    id: null,
    coachId: null,
    price: null,
    description: null,
    goodReview: null,
    classTotalNum: null,
    tag: [],
    point: null,
    ifRecommend: null,
    createTime: null,
    updateTime: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    coachId: null,
    price: null,
    description: null,
    goodReview: null,
    classTotalNum: null,
    tag: null,
    point: null,
    ifRecommend: null,
  },
  rules: {
    coachId: [
      { required: true, message: "教练id不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "教练价格不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询私教课程列表列表 */
function getList() {
  loading.value = true;
  listClass(queryParams.value).then(response => {
    classList.value = response.rows.map(item => {
      if (item.tag) {
        item.tag = JSON.parse(item.tag);
      }
      return item;
    });
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    coachId: null,
    price: null,
    description: null,
    goodReview: null,
    classTotalNum: null,
    tag: [],
    point: null,
    ifRecommend: null,
    createTime: null,
    updateTime: null
  };
  tagInput.value = "";
  proxy.resetForm("classRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加私教课程列表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getClass(_id).then(response => {
    form.value = response.data;
    form.value.tag = form.value.tag ? JSON.parse(form.value.tag) : [];
    open.value = true;
    title.value = "修改私教课程列表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["classRef"].validate(valid => {
    if (valid) {
      form.value.tag = JSON.stringify(form.value.tag);
      if (form.value.id != null) {
        updateClass(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClass(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除私教课程列表编号为"' + _ids + '"的数据项？').then(function() {
    return delClass(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/class/export', {
    ...queryParams.value
  }, `class_${new Date().getTime()}.xlsx`)
}

/** 添加标签 */
function addTag() {
  if (tagInput.value.trim() === "") {
    proxy.$modal.msgError("标签不能为空");
    return;
  }
  if (!form.value.tag.includes(tagInput.value)) {
    form.value.tag.push(tagInput.value);
    tagInput.value = "";
  } else {
    proxy.$modal.msgError("标签已存在");
  }
}

/** 删除标签 */
function removeTag(tag) {
  form.value.tag = form.value.tag.filter(t => t !== tag);
}

getList();
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
